<template>
  <div class="changePhone">
    <top-bar :title="'换绑手机'"/>
    <div class="content">
      <div class="content-top">
        <span>当前绑定手机{{ phoneNumbers }}</span>
      </div>
      <div class="content-bottom">
        <el-input v-model="input" placeholder="请输入验证码"></el-input>
        <span @click="getCode">{{ gain }}</span>
      </div>
    </div>
    <div class="next">
      <button @click="next">下一步</button>
    </div>
  </div>
</template>

<script>
import topBar from './topBar'
import {mapGetters} from 'vuex'
import * as api from '../../api'

export default {
  name: 'changePhone',
  components: {
    topBar
  },
  data () {
    return {
      phoneNumbers: this.$store.state.userDetail.mobile,
      input: '',
      gain: '获取验证码',
      sendStatus: true,
    }
  },
  computed: {
    actionStatus () {
      return this.$store.state.actionStatus
    },
    ...mapGetters(['succMsg', 'failMsg'])
  },
  watch: {
    actionStatus () {
      if (!this.actionStatus) return false
      this.smsCd()
    },
    succMsg () {
      this.succMsg && this.Toast(this.succMsg, 500)
    },
    failMsg () {
      this.failMsg && this.Toast(this.failMsg, 500)
    }
  },
  methods: {
    getCode () {
      this.$store.dispatch('fSmsSend', {
        'sms-type': 6,
        'sms-mobile': ''
      })
    },
    // 下一步
    next () {
      let input = this.input.trim()
      if (!input) {
        this.Toast('请输入验证码！', 500)
        return false
      }
      let obj = {
        'sms-type': 4,
        'sms-code': input,
        'sms-mobile': ''
      }
      api.smsCheckold(obj, succ => {
        this.$router.push({path: '/me/bindingPhone'})
      }, err => {
        this.Toast(err, 500)
      })
    },
    // 发送验证码
    smsCd () {
      if (!this.sendStatus) {
        return
      }
      this.sendStatus = false
      let t = 60
      this._t = window.setInterval(() => {
        t--
        if (t === 0) {
          clearInterval(this._t)
          this.sendStatus = true
          this.$store.state.actionStates = false
          this.gain = '获取验证码'
        } else {
          this.gain = t + '秒后重试'
        }
      }, 1000)
    },
  }
}
</script>

<style lang="scss" scoped>
.changePhone {
  padding: 0.75rem 0.48rem;

  .content {
    .content-top {
      width: 100%;
      height: 1.39rem;

      span {
        font-size: 0.32rem;
        font-family: SourceHanSansCN-Regular, SourceHanSansCN;
        font-weight: 400;
        color: #363739;
        margin-left: 0.4rem;
      }
    }

    .content-bottom {
      width: 100%;
      height: 1.39rem;
      background: rgba(255, 255, 255, 0.6);
      border-radius: 0.21rem;
      display: flex;
      align-items: center;

      /deep/ .el-input__inner {
        height: 1.39rem;
        border: none;
        padding-left: 0.4rem;
        border-radius: 0.21rem 0 0 0.21rem;
        font-size: 0.32rem;
      }

      > span {
        width: 2.22rem;
        font-size: 0.29rem;
        font-family: SourceHanSansCN-Regular, SourceHanSansCN;
        font-weight: 400;
        color: #FF1D1D;
        margin-right: 0.67rem;
      }
    }
  }

  .next {
    padding: 0 0.45rem;

    button {
      margin-top: 0.96rem;
      width: 8.13rem;
      height: 1.2rem;
      background: #FF751A linear-gradient(90deg, #FF0000 0%, #FFD500 100%);
      border-radius: 0.67rem;
      font-size: 0.45rem;
      font-family: SourceHanSansCN-Medium, SourceHanSansCN;
      font-weight: 500;
      color: #FFFFFF;
    }
  }
}
</style>
